package com.zf.flutter_app;

import android.content.Context;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;

import org.json.JSONObject;

import java.util.List;

import io.flutter.plugin.platform.PlatformView;

public class BDMapView implements PlatformView {

    private MapView mMapView;
    BaiduMap mBaiduMap;
    LocationClient mLocationClient;

    public BDMapView(Context context,MainActivity activity,List<MapData> datas) {
        MapView mapView = mMapView= new MapView(activity);
        BaiduMap baiduMap = mBaiduMap= mapView.getMap();
        baiduMap.setMyLocationEnabled(true);
        startLoc(activity);

        for(MapData mapData:datas) {
            LatLng point = new LatLng(Double.valueOf(mapData.lat), Double.valueOf(mapData.lng));
            BitmapDescriptor bitmap = BitmapDescriptorFactory
                    .fromResource(R.mipmap.like_selected);
            Bundle bundle = new Bundle();
            bundle.putString("shopName",mapData.name);
            OverlayOptions option = new MarkerOptions()
                    .position(point)
                    .title(mapData.name)
                    .extraInfo(bundle)
                    .icon(bitmap);
            mBaiduMap.addOverlay(option);
        }

        mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                try {
                    JSONObject json = new JSONObject();
                    LatLng latLng = marker.getPosition();
                    json.put("name", marker.getExtraInfo().getString("shopName"));
                    json.put("lat",latLng.latitude);
                    json.put("lng",latLng.longitude);
                    activity.mMessageChannel.send(json.toString());
                    return true;

                }catch (Exception e){
                    e.printStackTrace();
                    return false;
                }

            }
        });
    }

    @Override
    public View getView() {
        return mMapView;
    }

    @Override
    public void dispose() {
            mLocationClient.stop();
            mBaiduMap.setMyLocationEnabled(false);
    mMapView.onDestroy();
    mMapView = null;
    }

    private boolean isFirstIn = true;

    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            //mapView 销毁后不在处理新接收的位置
            if (location == null || mMapView == null){
                return;
            }
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(location.getDirection()).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);
            if(isFirstIn) {
                isFirstIn = false;
                LatLng newLocation = new LatLng(location.getLatitude(), location.getLongitude());
                MapStatusUpdate status2 = MapStatusUpdateFactory.newLatLng(newLocation);
                mBaiduMap.setMapStatus(status2);
            }
        }
    }

    public void startLoc(MainActivity activity){
        //定位初始化
        mLocationClient = new LocationClient(activity);

//通过LocationClientOption设置LocationClient相关参数
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);

//设置locationClientOption
        mLocationClient.setLocOption(option);

//注册LocationListener监听器
        MyLocationListener myLocationListener = new MyLocationListener();
        mLocationClient.registerLocationListener(myLocationListener);
//开启地图定位图层
        mLocationClient.start();
    }


}
